﻿#if UNITY_EDITOR
using NuoYan.Excel;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class ReadExcelDataBaseSOAssetsRightRect
{
    static ReadExcelDataBaseSOAssetsRightRect()
    {
        EditorApplication.projectWindowItemOnGUI += OnProjectWindowItemGUI;
    }

    private static void OnProjectWindowItemGUI(string guid, Rect selectionRect)
    {
        // 通过GUID获取对应的资源
        UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(AssetDatabase.GUIDToAssetPath(guid));

        // 只对AudioClip类型的资源生效
        if (asset is ReadExcelDataBaseSO data)
        {
            // 检查当前资源是否被选中
            bool isSelected = Selection.objects != null && System.Array.IndexOf(Selection.objects, asset) != -1;

            if (isSelected)
            {
                // 计算按钮位置（在资源项右侧）
                float buttonWidth = 80;
                Rect buttonRect = new Rect(
                    selectionRect.x + selectionRect.width - buttonWidth - 5, // 右侧偏移
                    selectionRect.y + 2,
                    buttonWidth,
                    selectionRect.height - 2
                );

                // 绘制按钮
                if (GUI.Button(buttonRect, "打开表格"))
                {
                    // 按钮点击逻辑
                    HandleReadExcelDataBaseSO(data);
                }
            }
        }
    }

    private static void HandleReadExcelDataBaseSO(ReadExcelDataBaseSO data)
    {
        EditorUtility.RevealInFinder(data.excelFilePath);
    }
}
#endif